package com.fsnbigdata.repository;
import com.fsnbigdata.domain.*;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = OrganismDocumentRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = OrganismDocumentRepositoryCustom.class)
@Transactional(readOnly = true)
public class OrganismDocumentRepositoryImpl extends QueryDslRepositorySupportExt<OrganismDocument> implements OrganismDocumentRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    OrganismDocumentRepositoryImpl() {
        super(OrganismDocument.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DOC_YEAR = "docYear";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String AUTHOR = "author";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM = "organism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    @Override
    public Page<OrganismDocument> findAll(Predicate predicate, Pageable pageable) {
        QOrganismDocument organismDocument = QOrganismDocument.organismDocument;
        JPQLQuery<OrganismDocument> query = from(organismDocument).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        return loadPage(query, pageable, organismDocument);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<OrganismDocument> findByOrganism(Organism organism, GlobalSearch globalSearch, Pageable pageable) {
        QOrganismDocument organismDocument = QOrganismDocument.organismDocument;
        JPQLQuery<OrganismDocument> query = from(organismDocument);
        Assert.notNull(organism, "organism is required");
        query.where(organismDocument.organism.eq(organism));
        Path<?>[] paths = new Path<?>[] { organismDocument.organism, organismDocument.name, organismDocument.author, organismDocument.docYear };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ORGANISM, organismDocument.organism).map(NAME, organismDocument.name).map(AUTHOR, organismDocument.author).map(DOC_YEAR, organismDocument.docYear);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, organismDocument);
    }
}
